package com.gzdh.dishrecognition.ui.adapter.paicai;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alipay.iot.bpaas.api.utils.StringUtil;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.model.HosMenuDto;
import com.gzdh.dishrecognition.model.MenuLBDto;
import com.gzdh.dishrecognition.model.NWeekMenuBakDto;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class LeftMenuAdapter extends RecyclerView.Adapter<LeftMenuAdapter.ViewHolder> {

    private final Context mContext;

    private int select = -1;

    List<HosMenuDto> list;

    List<HosMenuDto> oldList;

    MenuLBDto menuLBDto;

    private String search;

    private OnUserClick onUserClick;

    private List<NWeekMenuBakDto> nWeekMenuBakDtoList = new ArrayList<>();

    public void setnWeekMenuBakDtoList(List<NWeekMenuBakDto> nWeekMenuBakDtoList) {
        this.nWeekMenuBakDtoList = nWeekMenuBakDtoList;
    }

    public void setOnUserClick(OnUserClick onUserClick) {
        this.onUserClick = onUserClick;
    }

    public void setSearch(String search) {
        this.search = search;
    }

    public LeftMenuAdapter(Context context) {
        this.mContext = context;
        this.list = new ArrayList<>();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_food, parent, false);
        return new ViewHolder(itemView);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {

        holder.nameTv.setText(list.get(position).getName());
        GradientDrawable drawable = new GradientDrawable();
        drawable.setShape(GradientDrawable.RECTANGLE);
        drawable.setCornerRadius(10);
        drawable.setColor(Color.rgb(250, 250, 250));
        holder.mainLinear.setBackground(drawable);
        holder.mainLinear.setOnClickListener(view -> {
            if (onUserClick != null) {
                onUserClick.onUserClick(list.get(position));
            }
        });
        holder.price.setText(list.get(position).getPrice());

    }

    public void setList(List<HosMenuDto> list) {
        this.list = list;
        this.oldList = list;
    }

    public void setMenuLBDto(MenuLBDto menuLBDto) {
        this.menuLBDto = menuLBDto;
    }

    public void showNotify() {

        Stream<HosMenuDto> hosMenuDtoStream = oldList.stream();
        if (menuLBDto != null) {
            hosMenuDtoStream = hosMenuDtoStream.filter(hosMenuDto -> hosMenuDto.getLbbm().equals(menuLBDto.getLbbm()));
        }
        if (!StringUtil.isEmpty(search)) {
            hosMenuDtoStream = hosMenuDtoStream.filter(hosMenuDto -> hosMenuDto.getNamePinyin().contains(search));
        }
        hosMenuDtoStream = hosMenuDtoStream.filter(hosMenuDto ->
                !nWeekMenuBakDtoList.stream().map(NWeekMenuBakDto::getBm).collect(Collectors.toList()).contains(hosMenuDto.getBm())
        );
        list = hosMenuDtoStream.collect(Collectors.toList());
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mainLinear;
        TextView nameTv;

        TextView price;

        ViewHolder(View item) {
            super(item);
            mainLinear = item.findViewById(R.id.food_main);
            nameTv = item.findViewById(R.id.tv_name);
            price = item.findViewById(R.id.tv_price);
        }
    }

    public interface OnUserClick {
        public void onUserClick(HosMenuDto hosMenuDto);
    }

}
